package com.gitee.wsl.collections.set

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class MapBackedSetTest {

    @Test
    fun testOrdering() {

        val set = MapBackedSet.mapBackedSet(linkedMapOf<String,Any>())

        for (i in 0..9) {
            set.add(i.toString())
        }

        var it = set.iterator()

        for (i in 0..9) {
            assertEquals(i.toString(), it.next(), "Sequence is wrong")
        }

        run {
            var i = 0
            while (i < 10) {
                assertTrue(set.remove(i.toString()), "Must be able to remove int")
                i += 2
            }
        }

        it = set.iterator()
        run {
            var i = 1
            while (i < 10) {
                assertEquals(i.toString(), it.next(), "Sequence is wrong after remove ")
                i += 2
            }
        }

        for (i in 0..9) {
            set.add(i.toString())
        }

        assertEquals(10, set.size, "Size of set is wrong!")

        it = set.iterator()
        run {
            var i = 1
            while (i < 10) {
                assertEquals(i.toString(), it.next(), "Sequence is wrong")
                i += 2
            }
        }
        var i = 0
        while (i < 10) {
            assertEquals(i.toString(), it.next(), "Sequence is wrong")
            i += 2
        }
    }
}
